//
//  HMEmoticonToolBar.swift
//  Weibo
//
//  Created by teacher on 2017/6/14.
//  Copyright © 2017年 teacher. All rights reserved.
//

import UIKit

//  表情类型
enum HMEmoticonType: Int {
    //  最近
    case recent = 1000
    //  默认
    case normal = 1001
    //  emoji
    case emoji = 1002
    //  浪小花
    case lxh = 1003
}

//  表情键盘的toolbar视图
class HMEmoticonToolBar: UIStackView {
    //  记录上次选中的按钮
    var lastSelectedButton: UIButton?
    //  点击表情按钮回调的闭包
    var emoticonButtonCallBack: ((HMEmoticonType)->())?

    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func setupUI() {
    
        self.axis = .horizontal
        //  等比填充
        self.distribution = .fillEqually
        
        addChildButton(imageName: "compose_emotion_table_left", title: "最近", type: .recent)
        addChildButton(imageName: "compose_emotion_table_left", title: "默认", type: .normal)
        addChildButton(imageName: "compose_emotion_table_left", title: "Emoji", type: .emoji)
        addChildButton(imageName: "compose_emotion_table_left", title: "浪小花", type: .lxh)
        
    }
    
    /// 创建和添加子按钮
    ///
    /// - Parameters:
    ///   - imageName: 图片名
    ///   - title: 文字信息
    private func addChildButton(imageName: String, title: String, type: HMEmoticonType) {
    
        let button = UIButton()
        //  枚举的原始值作为tag值使用， 目的： 点击按钮通过tag获取对应枚举
        button.tag = type.rawValue
        //  添加点击事件
        button.addTarget(self, action: #selector(buttonAction(btn:)), for: .touchUpInside)
        //  设置不同状态的图片
        button.setBackgroundImage(UIImage(named: imageName + "_normal"), for: .normal)
        button.setBackgroundImage(UIImage(named: imageName + "_selected"), for: .selected)
        //  设置文字
        button.setTitle(title, for: .normal)
        button.titleLabel?.font = UIFont.systemFont(ofSize: 14)
        button.setTitleColor(UIColor.white, for: .normal)
        button.setTitleColor(UIColor.darkGray, for: .selected)
        //  取消点击高亮
        button.adjustsImageWhenHighlighted = false
        
        //  添加子视图
        addArrangedSubview(button)
        if type == .normal {
//            lastSelectedButton?.isSelected = false
            button.isSelected = true
            lastSelectedButton = button
        }
    }
    
    //  MARK:   --toolbar按钮点击事件处理
    @objc private func buttonAction(btn: UIButton) {
        //  如果点击的按钮和上一次选中的按钮相同直接返回
        if btn == lastSelectedButton {
            return
        }
        
        //  上一次取消选中
        lastSelectedButton?.isSelected = false
        //  点击按钮选中
        btn.isSelected = true
        //  记录这次选中的按钮
        lastSelectedButton = btn
        
        //  获取枚举类型
        let type = HMEmoticonType(rawValue: btn.tag)!
        //  执行闭包
        emoticonButtonCallBack?(type)
    }
    
    //  根据section选中指定按钮
    func selectedButtonWithSection(section: Int) {
        //  根据tag获取对应的子视图
        //  如果tag是0，获取的视图不是子视图，是当前视图自己
        //  注意点: 设置视图的tag的是不建议设置为0
        let button = self.viewWithTag(section + 1000) as! UIButton
        
        //  判断选中的按钮和上一次选中的按钮是否相同，相同直接返回
        if button == lastSelectedButton {
            return
        }
        
        lastSelectedButton?.isSelected = false
        button.isSelected = true
        lastSelectedButton = button
    }
}
